package com.qf.java2105.huangchuang.hotel.service.impl;

import com.qf.java2105.huangchuang.hotel.constant.MessageConstant;
import com.qf.java2105.huangchuang.hotel.dao.IFoodTypeDao;
import com.qf.java2105.huangchuang.hotel.entity.ResultVO;
import com.qf.java2105.huangchuang.hotel.factory.BeanFactory;
import com.qf.java2105.huangchuang.hotel.pojo.FoodType;
import com.qf.java2105.huangchuang.hotel.service.IFoodTypeService;

import java.sql.SQLException;
import java.util.List;

/**
 * 菜系业务层实现
 * @author hcvg
 * @version v1.0
 * @date 2021/09/11
 **/
public class FoodTypeServiceImpl implements IFoodTypeService {
    IFoodTypeDao foodTypeDao = (IFoodTypeDao) BeanFactory.getBean("foodTypeDao");

    @Override
    public ResultVO<List<FoodType>> findByFoodTypeName(String foodTypeName) {
        try {
            List<FoodType> foodTypeList = foodTypeDao.findByFoodTypeName(foodTypeName);
            return ResultVO.ok(MessageConstant.QUERY_ALL_FOOD_TYPE_SUCCESS, foodTypeList);
        } catch (SQLException e) {
            e.printStackTrace();
            return ResultVO.error(MessageConstant.QUERY_ALL_FOOD_TYPE_FAILED);
        }
    }

    @Override
    public ResultVO<FoodType> findFoodTypeById(Integer typeId) {
        try {
            FoodType foodType = foodTypeDao.findFoodTypeById(typeId);
            return ResultVO.ok(MessageConstant.QUERY_FOOD_TYPE_SUCCESS, foodType);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.error(MessageConstant.QUERY_FOOD_TYPE_FAILED);
        }
    }

    @Override
    public ResultVO<String> update(Integer foodTypeId, String foodTypeName) {
        try {
            foodTypeDao.update(foodTypeId, foodTypeName);
            return ResultVO.ok(MessageConstant.UPDATE_FOOD_TYPE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.error(MessageConstant.UPDATE_FOOD_TYPE_FAILED, e.getMessage());
        }
    }

    @Override
    public ResultVO<String> delete(Integer typeId) {
        try {
            foodTypeDao.delete(typeId);
            return ResultVO.ok(MessageConstant.DELETE_FOOD_TYPE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.error(MessageConstant.DELETE_FOOD_TYPE_FAILED, e.getMessage());
        }
    }

    @Override
    public ResultVO<String> save(String typeName) {
        try {
            foodTypeDao.save(typeName);
            return ResultVO.ok(MessageConstant.SAVE_FOOD_TYPE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.ok(MessageConstant.SAVE_FOOD_TYPE_FAILED, e.getMessage());
        }
    }

    @Override
    public ResultVO<String> foodTypeExist(String typeName) {
        try {
            Integer typeId = foodTypeDao.foodTypeExist(typeName);
            if (null != typeId){
                return ResultVO.error(MessageConstant.FOOD_TYPE_EXIST);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultVO.ok(MessageConstant.FOOD_TYPE_NOT_EXIST);
    }
}
